﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BB.Apostas.Filters;
using BB.Apostas.Models;
using DotNet.Highcharts.Options;
using DotNet.Highcharts.Helpers;
using DotNet.Highcharts;
using DotNet.Highcharts.Enums;
using WebMatrix.WebData;
using System.Drawing;

namespace BB.Apostas.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class EstatisticaController : Controller
    {
        private ApostasBalasDbContext db = new ApostasBalasDbContext();

        public ActionResult Index()
        {
            var competicaoactiva = db.Registos
              .Where(cr => cr.UserId == WebSecurity.CurrentUserId && cr.Active == true).SingleOrDefault();

            if (competicaoactiva == null)
                return View();

            var users = db.UserProfiles.ToList();
            Series[] allseries = new Series[users.Count];

            int j = 0;
            foreach (var user in users)
            {
                var lista = db.Classicacoes
               .Where(c => c.CompeticaoId == competicaoactiva.CompeticaoId && c.UserId == user.UserId)
               .OrderBy(c => c.Id)
               .ToList();

                object[,] classData = new object[lista.Count, 2];
                int i = 0;
                foreach (Classificacao item in lista)
                {
                    classData.SetValue(item.Jornada.Name, i, 0);
                    classData.SetValue(item.Score, i, 1);
                    i++;
                }
                allseries[j] = new Series { Data = new Data(classData), Name = user.UserName };
                j++;
            }

            Highcharts rank = new Highcharts("Percurso")
                .SetTitle(new Title
                {
                    Text = "Percurso no Rank",
                    X = -20
                })
                .SetSubtitle(new Subtitle
                {
                    Text = "Source: B&B",
                    X = -20
                })
                .SetLegend(new Legend
                {
                    Layout = Layouts.Vertical,
                    Align = HorizontalAligns.Right,
                    VerticalAlign = VerticalAligns.Top,
                    X = -10,
                    Y = 100,
                    BorderWidth = 0
                })
                .SetTooltip(new Tooltip
                {
                    Formatter = @"function() {
                                        return '<b>'+ this.series.name +'</b><br/>'+
                                    'Pontos' +': '+ this.y;
                                }"
                })
                .SetYAxis(new YAxis
                {
                    Title = new YAxisTitle { Text = "Pontos" },
                    PlotLines = new[]
                            {
                                new YAxisPlotLines
                                    {
                                        Value = 0,
                                        Width = 1,
                                        Color = ColorTranslator.FromHtml("#808080")
                                    }
                            }
                })
                .SetXAxis(new XAxis { Type = AxisTypes.Category })
                .SetSeries(allseries);

            return View(new Container(new[] { rank }));
        }
    }
}
